//
//  SCNForgetPswViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/18.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNForgetPswViewController: SCNTableViewController {

    lazy var phoneTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "手机号", placeholder: "请输入您的手机号")
        return tf
    }()
    lazy var securityCodeB: UIButton = {
        let btn = UIButton(type: .custom)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13.5)
        btn.setTitle("发送验证码", for: .normal)
        btn.setTitleColor(SCNConstants.rootBackgroundColor, for: .normal)
        btn.addTarget(self, action: #selector(send(_:)), for: .touchUpInside)
        return btn
        
    }()
    lazy var securityCodeTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "验证码", placeholder: "请输入短信验证码")
        return tf
    }()
    lazy var commitB: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = SCNConstants.rootBackgroundColor
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        btn.setTitle("提交", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.layer.cornerRadius = 5.0
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(commit(_:)), for: .touchUpInside)
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "忘记密码"
        
        view.addSubview(phoneTF)
        view.addSubview(securityCodeTF)
        view.addSubview(securityCodeB)
        view.addSubview(commitB)
        
        layout()
    }
    
    func layout() {
        
        phoneTF.snp.makeConstraints { (make) in
            make.top.equalTo(80)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        securityCodeB.snp.makeConstraints { (make) in
            
            make.top.equalTo(phoneTF.snp.bottom).offset(10)
            make.trailing.equalTo(view.snp.trailing).offset(-15)
            make.size.equalTo(CGSize(width: 70,height: 40))
            
        }
        securityCodeTF.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTF.snp.bottom).offset(10)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        commitB.snp.makeConstraints { (make) in
            make.top.equalTo(securityCodeTF.snp.bottom).offset(30)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
    }
    
    func send(_ sender: UIButton) {

        self.sendSecurityCode(phoneTF.text)
        
    }
    
    func commit(_ sender: UIButton) {
        
        if let tmpText = phoneTF.text,let tmpCode = securityCodeTF.text {
            guard !tmpText.isEmpty else {
                self.noticeOnlyText("请输入您的手机号")
                return
            }
            guard !tmpCode.isEmpty else {
                self.noticeOnlyText("请输入验证码")
                return
            }
            SCNRequestManager.postRequest(["mobile":tmpText,"code":tmpCode], URL: SCNRequestAPI.menber().forgetPassword, completeBlock: { (result) in
                
                let value = result.result.value as? Dictionary<String, Any>
                let dataresult = value?["dataresult"] as? Dictionary<String, Any>
                let msg = dataresult?["sucmsg"] as? String
                if let tmp = msg {
                    self.noticeOnlyText(tmp)
                    self.navigationController?.popViewController(animated: true)
                }
            })
        }
    }

}
